package com.douqu.game.core.config;

import com.douqu.game.core.config.common.CommonData;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangzhenfei
 *         2017-12-04 14:38
 */
public class PurchaseTimesConfig extends GameObject{
    /** 祭祀次数购买价格 **/
    public CommonData[] altarBuyPrice;
    public Map<Integer, Integer> altarBuyPriceMap = new HashMap<>();

    /** 购买竞技次数价格 **/
    public GoodsData[] buyArenaMax;

    /** 每日签到补签价格 **/
    public GoodsData[] reissueDailySign;

    /** 远征更换关卡费用 **/
    public GoodsData[] expeditionChange;

    /** 远征购买次数费用 **/
    public GoodsData[] expeditionBuyPrice;

    @Override
    public boolean check()
    {
        if(altarBuyPrice == null)
        {
            LogUtils.error("PurchaseTimesConfig check buyExpMax is null -> id:" + id + " name:" + name);
            return false;
        }

        if(buyArenaMax == null)
        {
            LogUtils.error("PurchaseTimesConfig check buyArenaMax is null -> id:" + id + " name:" + name);
            return false;
        }

        if(reissueDailySign == null)
        {
            LogUtils.error("PurchaseTimesConfig check reissueDailySign is null -> id:" + id + " name:" + name);
            return false;
        }

        if(expeditionChange == null)
        {
            LogUtils.error("PurchaseTimesConfig check expeditionChange is null -> id:" + id + " name:" + name);
            return false;
        }

        if(expeditionBuyPrice == null)
        {
            LogUtils.error("PurchaseTimesConfig check expeditionBuyPrice is null -> id:" + id + " name:" + name);
            return false;
        }
        return true;
    }

    @Override
    public boolean setVariable(String key, String value)
    {
        if("altarBuyPrice".equals(key))
        {
            altarBuyPrice = LoadUtils.loadCommonDataToArray(key, value);
            if(LoadUtils.checkCommonData(altarBuyPrice, this.getClass(), key, DataFactory.ALTAR_KEY, id, true))
            {
                for(CommonData data : altarBuyPrice)
                {
                    altarBuyPriceMap.put(data.id, data.value);
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        else if("buyArenaMax".equals(key))
        {
            buyArenaMax = LoadUtils.loadGoodDataToArray(key, value);
            return LoadUtils.checkGoods(buyArenaMax, this.getClass(), key, id);
        }
        else if("reissueDailySign".equals(key))
        {
            reissueDailySign = LoadUtils.loadGoodDataToArray(key, value);
            return LoadUtils.checkGoods(reissueDailySign, this.getClass(), key, id);
        }else if("expeditionChange".equals(key)){
            expeditionChange = LoadUtils.loadGoodDataToArray(key, value);
            return LoadUtils.checkGoods(expeditionChange, this.getClass(), key, id);
        }else if("expeditionBuyPrice".equals(key)){
            expeditionBuyPrice = LoadUtils.loadGoodDataToArray(key, value);
            return LoadUtils.checkGoods(expeditionBuyPrice, this.getClass(), key, id);
        }
        else
            return super.setVariable(key, value);
    }

    @Override
    public String toString() {
        return "PurchaseTimesConfig{" +
                "altarBuyPrice=" + Arrays.toString(altarBuyPrice) +
                ", altarBuyPriceMap=" + altarBuyPriceMap +
                ", buyArenaMax=" + Arrays.toString(buyArenaMax) +
                ", reissueDailySign=" + Arrays.toString(reissueDailySign) +
                ", expeditionChange=" + Arrays.toString(expeditionChange) +
                ", expeditionBuyPrice=" + Arrays.toString(expeditionBuyPrice) +
                "} " + super.toString();
    }
}
